package com.uenpay.im.ui.old.activity.chat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.qyx.android.weight.choosemorepic.PhotoAibum;
import com.qyx.android.weight.choosemorepic.PhotoItem;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.adapter.LocalImageAdapter;
import com.uenpay.im.entities.SendFile;
import com.uenpay.util.app.base.BaseActivity;

/**
 * @author SL
 * @date 2014/7/23
 * @copyRight http://www.qiyunxin.com
 */
public class LocalImageActivity extends BaseActivity {

    private PhotoAibum aibum;
    private GridView mGridView;
    private LocalImageAdapter adapter;
    private TextView sendTextView;
    private SendFile mSendFile = null;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.local_image_grid;
    }

    @Override
    public void initView(View view) {
        mGridView = (GridView) findViewById(R.id.local_image_gridview);
        title = (TextView) findViewById(R.id.title_textview);

        sendTextView = (TextView) findViewById(R.id.title_right_tv);
        sendTextView.setText(getResources().getString(R.string.send_file));
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
        aibum = (PhotoAibum) getIntent().getExtras().get("aibum");
        if (aibum != null) {
            title.setText(aibum.getName());
        } else {
            title.setText(getResources().getString(R.string.choose_send_image));
        }

        adapter = new LocalImageAdapter(this, aibum);
        mGridView.setAdapter(adapter);

        mGridView.setOnItemClickListener(gvItemClickListener);
        findViewById(R.id.title_right_layout).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mSendFile == null) {
                            return;
                        }
                        Intent data = new Intent();
                        data.putExtra("send_file", mSendFile);
                        setResult(RESULT_OK, data);
                        LocalImageActivity.this.finish();

                    }
                });
    }

    private OnItemClickListener gvItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            PhotoItem gridItem = aibum.getBitList().get(position);
            mSendFile = new SendFile();
            if (gridItem != null) {
                mSendFile.filePath = gridItem.getPath();
                mSendFile.fileName = gridItem.getName();
                mSendFile.fileSize = gridItem.getSize();
            }
            adapter.setSelectImage(position);
        }
    };

    @Override
    protected void onDestroy() {
        IMApplication.getInstance().removeActivity(this);
        super.onDestroy();
    }
}
